package se.janlindblom.ordning.typ;

/**
 * $Id: Id.java 58 2009-12-06 16:38:19Z lindblom.jan $
 * 
 * Copyright (c) 2009, Jan Lindblom
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * 
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * 
 * * Neither the name of the project nor the names of its contributors may be
 *   used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 */


/**
 * Id - Id number for a {@link Uppgift} or {@link Kategori}.
 * 
 * @author Jan Lindblom (lindblom.jan@gmail.com)
 * @version $Rev: 58 $
 */
public class Id {
	private int major;
	private int minor;
	private int micro;
	
	/**
	 * Creates an empty Id set to 0.0.0.
	 */
	public Id() {
		this.setMajor(0);
		this.setMinor(0);
		this.setMicro(0);
	}

	/**
	 * Creates an Id with the given major, minor and micro values.
	 * 
	 * @param major the major to set
	 * @param minor the minor to set
	 * @param micro the micro to set
	 */
	public Id(int major, int minor, int micro) {
		this();
		this.setMajor(major);
		this.setMinor(minor);
		this.setMicro(micro);
	}
	
	/**
	 * Returns the major id number of this Id.
	 * 
	 * @return the major
	 */
	public int getMajor() {
		return major;
	}
	
	/**
	 * Sets the major id number of this Id.
	 * 
	 * @param major the major to set
	 */
	public void setMajor(int major) {
		this.major = major;
	}
	
	/**
	 * Returns the minor id number of this Id.
	 * 
	 * @return the minor
	 */
	public int getMinor() {
		return minor;
	}
	
	/**
	 * Sets the minor id number of this Id.
	 * 
	 * @param minor the minor to set
	 */
	public void setMinor(int minor) {
		this.minor = minor;
	}
	
	/**
	 * Returns the micro id number of this Id.
	 * 
	 * @return the micro
	 */
	public int getMicro() {
		return micro;
	}
	
	/**
	 * Sets the micro id number of this Id.
	 * 
	 * @param micro the micro to set
	 */
	public void setMicro(int micro) {
		this.micro = micro;
	}
	
	/**
	 * Returns the String representation of this Id.
	 * 
	 * @return the String representation of this Id
	 */
	public String toString() {
		return this.getMajor() + "." + this.getMinor() + "." + this.getMicro();
	}

	/**
	 * Compares this Id to another Id.
	 * 
	 * @param id the Id to compare this Id to
	 * @return -1 if this < id, 0 if this == id and 1 if this > id.
	 */
	public int compareTo(Id id) {
		if (this.major < id.getMajor()) {
			return -1;
		} else if (this.major > id.getMajor()) {
			return 1;
		} else {
			if (this.minor < id.getMinor()) {
				return -1;
			} else if (this.minor > id.getMinor()) {
				return 1;
			} else {
				if (this.micro < id.getMicro()) {
					return -1;
				} else if (this.micro > id.getMicro()) {
					return 1;
				} else {
					return 0;
				}
			}
		}
	}
	
	/**
	 * Checks if this Id is equal to a given Id.
	 * 
	 * @param id the Id to compare with.
	 * @return true if this == id, false otherwise.
	 */
	public boolean equals(Id id) {
		if (this.compareTo(id) == 0)
			return true;
		return false;
	}
}
